
package my.algorithms.mcorrea.datastructures;
/**
 *
 * @author mcorrea
 */
public class Stack {
	
	private int top;
	private int[] array;
	private int MAX_CAPACITY = 10;
        private int[] arrayMin;
        private int topMin;
	
	public Stack(){
		this.array = new int[MAX_CAPACITY];
		this.top = -1; 
                this.arrayMin = new int[MAX_CAPACITY];
		this.topMin = -1; 
	}
	
	
	public void push(int x){
		if(top == array.length-1 ){
			System.err.println("Error: stack is full");
		}else{
			top = top + 1;
			array[top] = x;
                        //do the minimum operations
                        topMin +=1;
                        if(topMin !=0)
                            arrayMin[topMin] = Math.min(x, arrayMin[topMin-1]);
                        else
                            arrayMin[topMin] = x;
                        
		}
		
	}
	
	
	public int pop(){
		
		if(isEmpty()){
			System.err.println("Error: Stack is empty...");
			return -1;
		}else{
			int x = array[top];
			top = top - 1; 
                        //do operations for minimun
                        topMin -=1;
                        
			return x; 
		}
	}
        
        public int min(){
            if(topMin == -1){
                System.err.println("Error: Stack is empty...");
                return -1;
            }
            return arrayMin[topMin];
        }
	
	
	public boolean isEmpty(){
	
		if(top == -1){
			return true;
		}
		return false;
	}
	
         @Override
        public String toString(){
            String str = "[";
            for(int x: array){
                str +=x+" ";
            }
            return str+"]";           
        }
         
         public void minPrint(){
            String str = "[";
            for(int x: arrayMin){
                str +=x+" ";
            }
            str+="]";
            System.out.println(str);
            
        } 
        
        
        public static void main(String args[]){
            Stack stack = new Stack();
            System.out.println(stack);
            stack.push(6);
            stack.push(5);
            stack.push(4);
            System.out.println(stack);
            System.out.println(stack.min());
            stack.minPrint();
            stack.pop();
            System.out.println(stack);
            System.out.println(stack.min());
            stack.pop();
            System.out.println(stack);
            System.out.println(stack.min());
            stack.push(2);
            stack.push(1);
            System.out.println(stack);
            System.out.println(stack.min());
            stack.pop();
            System.out.println(stack);
            System.out.println(stack.min());
            
        }
	
	
	
}
